package jbelab3productmanager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Category {
	private int cateCode;
	private String cateName;
	private String description;
	private Product[] arr_products = new Product[2];
	private Category[] arr_category = new Category[2];
	private BufferedReader br;

	public Category() {
		super();
	}

	public Category(int cateCode, String cateName, String description) {
		super();
		this.cateCode = cateCode;
		this.cateName = cateName;
		this.description = description;
	}

	public int getCateCode() {
		return cateCode;
	}

	public void setCateCode(int cateCode) {
		this.cateCode = cateCode;
	}

	public String getCateName() {
		return cateName;
	}

	public void setCateName(String cateName) {
		this.cateName = cateName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	void addCate() {
		try {
			int catID;
			String catName;
			String catDesc;
			for (int i = 0; i < arr_category.length; i++) {				
				br = new BufferedReader(new InputStreamReader(System.in));
				System.out.println("        ADD NEW CATEGORY");
				System.out.println("================================");
				System.out.print("Enter Category ID: ");							
				catID = Integer.parseInt(br.readLine());
				System.out.print("Enter Category Name: ");
				catName = br.readLine(); 
				System.out.print("Enter Description: ");
				catDesc = br.readLine();
				arr_category[i] = new Category(catID, catName, catDesc);				
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	void showCate() {
		System.out.println("        CATEGORY INFOR");
		System.out.println("================================");
		for (int i = 0; i < arr_category.length; i++) {
			System.out.println("Category ID: " + arr_category[i].getCateCode());
			System.out.println("Category Name: "
					+ arr_category[i].getCateName());
			System.out.println("Description: "
					+ arr_category[i].getDescription());
			System.out.println("================================");
		}
	}

	void editCateName() throws IOException, Exception {
		int cateID;
		String newCateName;
		showCate();
		System.out.println("                EDIT CATEGORY NAME");
		System.out
				.println("=============================================================");
		System.out
				.println("Enter Category ID which you want to change Category Name: ");
		cateID = getCateID();
		for (int i = 0; i < arr_category.length; i++) {
			if (arr_category[i].cateCode == cateID) {
				System.out.println("Enter new Category Name: ");
				newCateName = br.readLine();
				arr_category[i].setCateName(newCateName);
			}
		}
	}

	int getCateID() throws Exception, IOException {
		int cateID = 0;
		cateID = Integer.parseInt(br.readLine());
		return cateID;
	}

	void addArrProduct() throws NumberFormatException, IOException {
		for (int i = 0; i < arr_category.length; i++) {
			System.out.println("Add Product for "+arr_category[i].cateName);
			for (int j = 0; j < arr_products.length; j++) {
				arr_products[j] = new Product();
				arr_category[i].arr_products[j] = arr_products[j].addProduct();
			}
		}
	}
	
	void showArrProduct(){
		for (int i = 0; i < arr_category.length; i++) {
			System.out.println("Product of "+ arr_category[i].getCateName());
			for (int j = 0; j < arr_products.length; j++) {				
				arr_category[i].arr_products[j].showProduct();
			}
		}
	}

}
